<?php
/*
 * Copyright (c) 2009 Tom Smith (tom@takeontom.com).
 * 
 * Distributed under the terms of the GNU Lesser General Public License.
 * 
 * ---------------------------------------------------------------------------------
 * 
 * This file is part of Seraph PHP Framework.
 *
 * Seraph PHP Framework is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Seraph PHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seraph PHP Framework.  If not, see <http://www.gnu.org/licenses/>.
*/


class MySQLiDBConnection extends DBConnection	{
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#executeSQL()
	 */
	public function executeSQL($sql, DBResult $result)	{
		$db = $this->getConnection();
		
		$res = $db->query($sql);
		
		$result->setRawResult($res);
		
		if ($db->error)	{
			$result->setErrorMessage($db->error);
			$result->setErrorNumber($db->errno);
			$result->setSuccess(false);
			return false;
		}
		
		$result->setSuccess(true);
		return true;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#doConnect()
	 */
	public function doConnect()	{
		$db = new mysqli($this->host, $this->user, $this->pass, $this->schema);
		$this->setConnection($db);
		
		if ($db->connect_error)	{
			$this->connectError = $db->connect_error;
			return false;
		}
		
		return true;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#doDisconnect()
	 */
	public function doDisconnect()	{
		$this->getConnection()->close();
		unset($this->connection);
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#getLastInsertID()
	 */
	public function getLastInsertID()	{
		return $this->getConnection()->insert_id;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#makeNewResultObject()
	 */
	public function makeNewResultObject()	{
		return new MySQLiDBResult($this);
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#escapeString()
	 */
	public function escapeString($string)	{
		return $this->getConnection()->real_escape_string($string);
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#getConnectError()
	 */
	public function getConnectError()	{
		return $this->connectError;
	}
	
	/* (non-PHPdoc)
	 * @see src/framework/DB/DBConnection#getTableNames()
	 */
	public function getTableNames()	{
		$sql = 'SHOW TABLES;';
		$res = $this->query($sql);
		
		if (!$res->isSuccess())	{
			throw new RuntimeException('Unable to retrieve table names.');
			return array();
		}
		
		$out = array();
		foreach($res as $aRow)	{
			$out[] = current($aRow);
		}
		return $out;
	}
}
?>